package org.example;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @program: itcast-MyRPC
 * @description:
 * @author: zhanghz001
 * @create: 2021-02-19 14:21
 **/
public class SocketServer {
    public static void main(String[] args) throws IOException {
        //创建socket服务器,添加监听端口
        ServerSocket serverSocket = new ServerSocket(65535);
        //输入流对象读取数据
        byte[] bytes = new byte[1024];
        //监听client
        while (true) {
            Socket accept = null;
            InputStream inputStream = null;
            try {
                System.out.println("服务器阻塞中,等待客户端连接");
                //获取到客户端的socket链接请求,是阻塞方法
                accept = serverSocket.accept();
                // TimeUnit.SECONDS.sleep(60);
                //获取输入流
                inputStream = accept.getInputStream();
                int read = inputStream.read(bytes);
                //打印输出数据
                System.out.println(new String(bytes, 0, read));
                System.out.println("服务端关闭链接");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭资源
                if (accept != null) {
                    accept.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                    
                }
            }
        }
        
    }
}
